﻿#define _CRT_SECURE_NO_WARNINGS 1
#include "Snake.h"
#define KEY_PRESS(VK) ( (GetAsyncKeyState(VK) & 0x1) ? 1 : 0 )

void SetPos(short x, short y)
{
	COORD pos = { x,y }; //⼀个字符在控制台屏幕缓冲区上的坐标
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);//获取 标准输出 的句柄
	SetConsoleCursorPosition(houtput, pos);//设置 标准输出上 光标的位置为pos
}

void WelcomeToGame()
{
	SetPos(36, 14);
	printf("欢迎来到贪吃蛇小游戏");
	SetPos(40, 25);//让按任意键继续的出现的位置好看点
	system("pause");
	system("cls");
	SetPos(20, 10);
	printf("用 ↑ . ↓ . ← . → 分别控制蛇的移动， F3为加速，F4为减速");
	SetPos(20, 11);
	printf("加速将能得到更高的分数");
	SetPos(40, 25);//让按任意键继续的出现的位置好看点
	system("pause");
	system("cls");
}

void CreateMap()
{
	int i = 0;
	for (i = 1; i < 30; i++)
	{
		wprintf(L"%lc", WALL);
	}
	SetPos(0, 26);
	for (i = 1; i < 30; i++)
	{
		wprintf(L"%lc", WALL);
	}
	for (i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	for (i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
	//getchar();//不让程序结束，检测代码是否执行正确
}

void InitSnake(pSnake ps)
{
	pSnakeNode cur = NULL;
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		cur->x = POS_X + i * 2;
		cur->y = POS_Y;
		cur->next = NULL;
		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}

	//打印蛇的身体
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//getchar();//不让程序结束，检测代码是否执行正确

	//初始化贪吃蛇数据
	ps->_dir = RIGHT;
	ps->_food_weight = 10;
	ps->_score = 0;
	ps->_sleep_time = 200;//ms
	ps->_status = OK;
}

void CreateFood(pSnake ps)
{
	ps->_pFood = (pSnakeNode)malloc(sizeof(SnakeNode));//申请节点，保存食物的信息
	if (ps->_pFood == NULL)
	{
		perror("CreateFood()::malloc()");
		return;
	}
	ps->_pFood->next = NULL;
	if (ps->_pFood == NULL)
	{
		perror("CreatFood()::malloc()");
		return;
	}
again:
	do
	{
		ps->_pFood->x = rand() % 53 + 2;
		ps->_pFood->y = rand() % 25 + 1;
	} while (ps->_pFood->x % 2 != 0);
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (cur->x == ps->_pFood->x && cur->y == ps->_pFood->y)
			goto again;
		cur = cur->next;
	}
	SetPos(ps->_pFood->x, ps->_pFood->y);
	wprintf(L"%lc", FOOD);
}

void GameStart(pSnake ps)
{
	//0. 先设置窗口的大小，设置窗口的名字
	system("mode con cols=100 lines=30");//=紧挨着，中间用空格
	system("title 贪吃蛇");
	//getchar();//不让程序结束，检测代码是否执行正确

	//1. 光标隐藏
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);//获取 标准输出 的句柄
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(houtput, &CursorInfo);//获取 控制台 光标信息
	CursorInfo.bVisible = false;//隐藏 控制台 光标
	SetConsoleCursorInfo(houtput, &CursorInfo);//设置 控制台光标状态
	//getchar();//不让程序结束，检测代码是否执行正确

	//2. 打印环境界面和功能介绍
	WelcomeToGame();
	//3. 绘制地图
	CreateMap();
	//4. 创建蛇
	InitSnake(ps);
	//5. 创建食物
	CreateFood(ps);
}

void PrintHelpInfo()
{
	SetPos(62, 16);
	printf("不能穿墙，不能咬到自己");
	SetPos(62, 17);
	printf("用 ↑ . ↓ . ← . → 分别控制蛇的移动");
	SetPos(62, 18);
	printf("F3为加速，F4为减速");
	SetPos(62, 19);
	printf("按ESC退出游戏，按空格暂停游戏");

	SetPos(62, 24);
	printf("Lzc制作@版权");
}

void Pause()
{
	while (1)
	{
		Sleep(200);//ms
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

int NextIsFood(pSnakeNode pn, pSnake ps)
{
	//&& 如果条件判断结果为 true 就返回第二个操作数的值，如果为 false 就返回第一个操作数的值。
	return (pn->x == ps->_pFood->x && pn->y == ps->_pFood->y);
}

void EatFood(pSnake ps)
{
	//头插
	ps->_pFood->next = ps->_pSnake;
	ps->_pSnake = ps->_pFood;

	pSnakeNode cur = ps->_pSnake;

	//打印蛇
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	
	//加分
	ps->_score += ps->_food_weight;

	//重新创建食物
	CreateFood(ps);
}

void NoFood(pSnakeNode pn, pSnake ps)
{
	//头插
	pn->next = ps->_pSnake;
	ps->_pSnake = pn;

	pSnakeNode cur = ps->_pSnake;

	//打印蛇头
	SetPos(cur->x, cur->y);
	wprintf(L"%lc", BODY);

	//得到倒数第二个节点
	while (cur->next->next != NULL)
	{
		cur = cur->next;
	}

	//先把这个位置打印成空格，因为已经打印了BODY
	SetPos(cur->next->x, cur->next->y);
	printf("  ");

	//free最后一个节点
	free(cur->next);
	cur->next = NULL;
}

void KillByWall(pSnake ps)
{
	if (ps->_pSnake->x == 0 ||
		ps->_pSnake->x == 56 ||
		ps->_pSnake->y == 0 ||
		ps->_pSnake->y == 26)
	{
		ps->_status = KILL_BY_WALL;
	}
}

void KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if (cur->x == ps->_pSnake->x && cur->y == ps->_pSnake->y)
		{
			ps->_status = KILL_BY_SELF;
			return;
		}
		cur = cur->next;
	}
}

void SnakeMove(pSnake ps)
{
	//创建一个结点，表示蛇即将到的下一个节点
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}

	if (ps->_dir == UP)
	{
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y =  ps->_pSnake->y - 1;
	}
	else if (ps->_dir == DOWN)
	{
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
	}
	else if (ps->_dir == LEFT)
	{
		pNextNode->x = ps->_pSnake->x - 2;
		pNextNode->y = ps->_pSnake->y;
	}
	else if (ps->_dir = RIGHT)
	{
		pNextNode->x = ps->_pSnake->x + 2;
		pNextNode->y = ps->_pSnake->y;
	}

	//判断下个节点是不是食物
	if (NextIsFood(pNextNode, ps))
	{
		EatFood(ps);

		//释放pNextNode
		free(pNextNode);
		pNextNode = NULL;
	}
	else
	{
		NoFood(pNextNode, ps);
	}

	KillByWall(ps);

	KillBySelf(ps);
}

void GameRun(pSnake ps)
{
	//打印右侧帮助信息
	PrintHelpInfo();

	do
	{
		//打印总分数和食物的分值
		SetPos(62, 11);
		printf("总分数:>%d",ps->_score);
		SetPos(62, 13);
		printf("当前食物的分数:>%2d", ps->_food_weight);

		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN)
		{
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP)
		{
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			//正常退出游戏
			ps->_status = END_NORMAL;
		}
		else if (KEY_PRESS(VK_F3))
		{
			//F3加速
			if (ps->_sleep_time > 100)
			{
				ps->_sleep_time -= 30;//ms
				ps->_food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			//F4减速
			if (ps->_sleep_time < 300)
			{
				ps->_sleep_time += 30;//ms
				ps->_food_weight -= 2;
			}
		}

		SnakeMove(ps);//蛇走一步的过程

		Sleep(ps->_sleep_time);

	} while (ps->_status == OK);
}

void GameEnd(pSnake ps)//游戏善后的工作
{
	pSnakeNode cur = ps->_pSnake;
	SetPos(24, 12);
	if (ps->_status == KILL_BY_WALL)
	{
		printf("您撞墙，游戏结束");
		return;
	}
	else if (ps->_status == KILL_BY_SELF)
	{
		printf("您吃到自己了，游戏结束");
		return;
	}
	else if (ps->_status == END_NORMAL)
	{
		printf("您主动退出了游戏");
		return;
	}

	//销毁节点,cur跳出就为NULL
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
}